package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.BlackListDao;
import com.huilian.iotbox.admin.wap.service.BlackListService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.BlackListDto;
import com.huilian.iotbox.data.vo.BlackListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-25 16:23
 * @Description:
 */
@Service
public class BlackListServiceImpl implements BlackListService {
    @Autowired
    private BlackListDao blackListDao;
    @Override
    public ServerResponse insert(BlackListDto blackListDto) {
        Integer count = this.findByMemberAndUserIdCount(blackListDto);
        if(count > 0){
            return ServerResponse.createByErrorMessage("当前会员已经在黑名单了,无法重复添加!");
        }
        Integer result = blackListDao.insert(blackListDto);
        if(result <= 0){
            return ServerResponse.createByErrorMessage("添加黑名单失败,请稍后尝试");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public Integer findByMemberAndUserIdCount(BlackListDto blackListDto) {
        return blackListDao.findByMemberAndUserIdCount(blackListDto);
    }

    @Override
    public ServerResponse delete(BlackListDto blackListDto) {
        Integer count = this.findByMemberAndUserIdCount(blackListDto);
        if(count <= 0){
            return ServerResponse.createByErrorMessage("当前会员不在黑名单内,无法移出黑名单!");
        }
        Integer result = blackListDao.delete(blackListDto);
        if(result <= 0){
            return ServerResponse.createByErrorMessage("移出黑名单失败,请稍后尝试");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public PageInfo findListPages(BlackListDto blackListDto) {
        PageHelper.startPage(blackListDto.getPageNum(),blackListDto.getPageSize()," bl.createTime desc");
        List<BlackListVo> list = blackListDao.findListPages(blackListDto);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
